<?php

class Logger
{
    const TYPE_BIZ_PROCESS 	= 'biz_process';
    const TYPE_BIZ_ERROR	= 'biz_error';
    const TYPE_SQL_INSERT	= 'sql_insert';
    const TYPE_SQL_UPDATE	= 'sql_update';
    const TYPE_SQL_EXECUTE	= 'sql_execute';
    const TYPE_SQL_ERROR	= 'sql_error';
    const TYPE_LOGIN_COST	= 'login_cost';
    const TYPE_DB_CONNECT	= 'db_connect';
    const TYPE_EXCEPTION	= 'exception';
    const TYPE_PDO			= 'pdo_connect';
    const TYPE_OTHER		= 'other';

    static private function getLogFile($log_type)
    {/*{{{*/
        $conf = QFrameConfig::getConfig('EXTERNALS');
        $dir = $conf['logPath'];
        $file = $dir . $log_type . '.log.' . date('Ymd');
        return $file;
    }/*}}}*/

    static public function writeLog($log_type, $msg)
    {/*{{{*/
        if($_SERVER['DOCUMENT_ROOT'] == '')
        {
            return false; // 命令行下运行
        }
        $file = self::getLogFile($log_type);
        if(is_array($msg))
        {
            $msg = var_export($msg, true);
        }
        $ip = @$_SERVER['REMOTE_ADDR'];
        $msg = $ip . ' ' . date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
        return error_log($msg, 3, $file);
    }/*}}}*/

    static public function writeBizProcessLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_BIZ_PROCESS, $msg);
    }/*}}}*/

    static public function writeBizErrorLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_BIZ_ERROR, $msg);
    }/*}}}*/

    static public function writeSqlInsertLog($sql, $value=false)
    {/*{{{*/
        $msg = $sql;
        if($value)
        {
            $msg = array('sql' => $sql, 'value' => $value);
        }
        return self::writeLog(self::TYPE_SQL_INSERT, $msg);
    }/*}}}*/

    static public function writeSqlUpdateLog($sql, $value=false)
    {/*{{{*/
        $msg = $sql;
        if($value)
        {
            $msg = array('sql' => $sql, 'value' => $value);
        }
        return self::writeLog(self::TYPE_SQL_UPDATE, $msg);
    }/*}}}*/

    static public function writeSqlExecuteLog($sql, $value=false)
    {/*{{{*/
        $msg = $sql;
        if($value)
        {
            $msg = array('sql' => $sql, 'value' => $value);
        }
        return self::writeLog(self::TYPE_SQL_EXECUTE, $msg);
    }/*}}}*/

    static public function writeSqlErrorLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_SQL_ERROR, $msg);
    }/*}}}*/

    static public function writeLoginCostLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_LOGIN_COST, $msg);
    }/*}}}*/

    static public function writeDbConnectLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_DB_CONNECT, $msg);
    }/*}}}*/

    static public function writeExceptionLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_EXCEPTION, $msg);
    }/*}}}*/

    static public function writeOtherLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_OTHER, $msg);
    }/*}}}*/

    static public function writePDOLog($msg)
    {/*{{{*/
        return self::writeLog(self::TYPE_PDO, $msg);
    }/*}}}*/
}

?>
